package com.quan.weixin.web.controller.boss;

import javax.validation.Valid;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.quan.base.common.base.web.support.BaseResult;
import com.quan.base.common.base.web.support.controller.BaseController;
import com.quan.base.common.utils.RandomUitl;
import com.quan.upms.annotation.RequiresPermissions;
import com.quan.weixin.common.WeixinContext;
import com.quan.weixin.model.ResponseMsg;
import com.quan.weixin.rpc.api.WeixinApiService;
import com.quan.weixin.rpc.api.WeixinMenuService;
import com.quan.weixin.rpc.model.WeixinMenu;

@RestController
@RequestMapping(value = "/api/weixin/menu")
public class WeixinMenuController extends BaseController {

	@Autowired
	private WeixinMenuService weixinMenuService;
	
	@Autowired
	private WeixinApiService apiService ;

	@RequiresPermissions(value="weixin:menu:add", description="添加微信菜单")
	@PostMapping(value = "/add")
	public Object add(@Valid @RequestBody WeixinMenu entity, BindingResult bindingResult) {
		if (bindingResult.hasErrors()) {
            return BaseResult.failure(bindingResult.getFieldError().getDefaultMessage()) ;
        }
		entity.setEventKey(StringUtils.isNotEmpty(entity.getType()) ? entity.getType().toUpperCase() + "_" + RandomUitl.generateString(5).toUpperCase() : null);
		this.weixinMenuService.insertSelective(entity);

		return BaseResult.ok();
	}

	@RequiresPermissions(value="weixin:menu:delete", description="删除微信菜单")
	@GetMapping(value = "/delete/{id}")
	public Object delete(@PathVariable Integer id) {
		this.weixinMenuService.delete(id);
		return BaseResult.ok();
	}

	@RequiresPermissions(value="weixin:menu:update", description="修改微信菜单")
	@PostMapping(value = "/update")
	public Object update(@Valid @RequestBody WeixinMenu entity, BindingResult bindingResult) {
		if (bindingResult.hasErrors()) {
            return BaseResult.failure(bindingResult.getFieldError().getDefaultMessage()) ;
        }
		if(entity.getPid() == null) {
			entity.setPid(0);
		}
		this.weixinMenuService.update(entity);

		return BaseResult.ok();
	}

	@RequiresPermissions(value="weixin:menu:detail", description="查询微信菜单详情")
	@GetMapping(value = "/get/{id}")
	public Object list(@PathVariable Integer id) {
		return this.weixinMenuService.selectById(id);
	}
	
	@RequiresPermissions(value="weixin:menu:list", description="查询微信菜单")
	@GetMapping(value = "/list")
	public Object list(WeixinMenu entity) {
		Integer pid = 0 ;
		if(entity.getPid() != null) {
			pid = entity.getPid();
		}
		return this.weixinMenuService.selectChildren(pid);
	}

	@RequiresPermissions(value="weixin:menu:tree", description="微信菜单树状")
	@GetMapping(value = "/tree")
	public Object tree(WeixinMenu entity) {
		return this.weixinMenuService.tree(entity);
	}
	
	/**
	 * 获取节点的所有父节点
	 * @param id
	 * @return
	 */
	@GetMapping(value = "/getParents")
	public Object getParents(Integer id) {
		return this.weixinMenuService.getParents(id);
	}
	
	/**
	 * 推送菜单到微信
	 * @return
	 */
	@RequiresPermissions(value="weixin:menu:pushMenuToWeiXin", description="微信菜单推送")
	@GetMapping(value="/pushMenuToWeiXin")
	public Object pushMenuToWeiXin() {
		ResponseMsg msg = this.apiService.pushMenuToWeiXin(WeixinContext.getAccessToken());
		
		if(msg.getErrcode() > 0) {
			logger.info("微信菜单推送失败：{}", msg.toString());
			return BaseResult.failure(msg.getErrcode() + ":" +msg.getErrmsg());
		} else {
			logger.info("微信菜单推送成功：{}", msg.toString());
			return BaseResult.ok();
		}
	}
}
